﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TLECDataLayer;

namespace EventManager
{
    public partial class EventsForm : Form
    {
        private TLECDataLayer.Event selectedEvent;

        public EventsForm()
        {
            InitializeComponent();
            WinLoad();
        }

        private void WinLoad()
        {
            this.eventBindingSource.DataSource = TLECDataLayer.Events.GetAllEvents();
            this.locationBindingSource.DataSource = Locations.GetAllLocations();
            this.categoryBindingSource.DataSource = Categories.GetAllCategories();
            EraseDataFromForms();
        }

        private void EraseDataFromForms()
        {
            this.locComboBox.SelectedItem = null;
            this.catComboBox.SelectedItem = null;
            this.titleTxt.Text = "";
            this.priceTxt.Text = "";
            this.dateTimePicker.Value = DateTime.Now;
        }

        private void eventsDGV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != this.eventsDGV.Rows.Count - 1 && e.RowIndex != -1)
            {
                selectedEvent = TLECDataLayer.Events.GetEventById(((TLECDataLayer.Event)
                    this.eventsDGV.Rows[e.RowIndex].DataBoundItem).EventId);
                this.locComboBox.SelectedValue = selectedEvent.LocationId;
                this.catComboBox.SelectedValue = selectedEvent.CategoryId;
                this.titleTxt.Text = selectedEvent.Title;
                this.priceTxt.Text = string.Format("{0}", selectedEvent.Price);
                this.dateTimePicker.Value = selectedEvent.DateAndTime;
            }
            else
            {
                selectedEvent = null;
                EraseDataFromForms();
            }
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            if (selectedEvent != null)
            {
                if (Reservations.GetAllReservationsByEvent(selectedEvent.EventId).Count() == 0)
                {
                    TLECDataLayer.Events.DeleteEvent(selectedEvent.EventId);
                    MessageBox.Show("The selected item has been deleted");
                }
                else
                {
                    MessageBox.Show("You can not delete this event because there are reservations for it!");
                }
            }
            else
            {
                MessageBox.Show("Select some item first.");
            }
            WinLoad();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (selectedEvent != null)
                {
                    TLECDataLayer.Events.UpdateEvent(selectedEvent.EventId, this.titleTxt.Text,
                        (int)this.locComboBox.SelectedValue, (int)this.catComboBox.SelectedValue,
                        Convert.ToDecimal(this.priceTxt.Text), this.dateTimePicker.Value);

                    MessageBox.Show("Changes have been saved");
                }
                else
                {
                    TLECDataLayer.Events.CreateEvent(this.titleTxt.Text,
                        (int)this.locComboBox.SelectedValue, (int)this.catComboBox.SelectedValue,
                        Convert.ToDecimal(this.priceTxt.Text), this.dateTimePicker.Value);

                    MessageBox.Show("New event has been created");
                }
                WinLoad();
            }
            else
            {
                MessageBox.Show("Invalid data in some form!");
            }      
        }

        private bool ValidateData()
        {
            if (!string.IsNullOrEmpty(this.titleTxt.Text) && !string.IsNullOrEmpty(this.priceTxt.Text))
            { 
                decimal pf;
                if (decimal.TryParse(this.priceTxt.Text, out pf) && this.locComboBox.SelectedValue != null && 
                    this.catComboBox.SelectedValue != null)
                {
                    return true;
                }
            }

            return false;
        }
    }
}
